<?php
require_once dirname(__FILE__) . '/../includes/common.inc.php';
require_once dirname(__FILE__) . '/../includes/pdoorm.inc.php';

header('Content-Type: text/plain');



  
try {
  $action = MiscUtils::getParam('action', NULL);
  $condition = MiscUtils::getParam('c', 'WHERE 1 = 1');
  $e_oid = MiscUtils::getParam('oid', NULL);
  
    switch($action){
    	case'v1sample':
    		samplefind($action,$condition,$e_oid);
    		break;
    	default:
    		 pdofind($action,$condition,$e_oid);
    		break; 	
    	
    }

   
} catch (PDOException $e) {
  echo $e->getMessage();
}


/**
 *function list  
 *2011年8月2日 14:11:59 ns.lanbinbin 
**/

function pdofind($action,$condition,$e_oid){

  $condition .= ($e_oid) ? ' AND u.e_oid = \'' . $e_oid . '\'' : '';
  
  $result = new stdClass();
  $result->data = array();

  
    $sql2 = "SELECT u.e_oid as id, u.* FROM ns_".$action." as u $condition";
    
    $tmp=getall($sql2);
    

    
    foreach($tmp as $tmp2){
    	
    	switch($action){
    		case'datezone':
    			{
    				$tmp2->start = SimpleDate::fromStamp($tmp2->start);
    				$tmp2->end = SimpleDate::fromStamp($tmp2->end);
    			}
    			break;
    			
    		default:
    			break;	
    	}
    	
    	$result->data[] = $tmp2;
    }
    
  echo json_encode($result); 
	
}


 function samplefind($action,$condition,$e_oid){

  $condition .= ($e_oid) ? ' AND u.e_oid = \'' . $e_oid . '\'' : '';
  
  
  $result = new stdClass();
  $result->data = array(); 
  
  
    $sql2 = "SELECT u.e_oid as id, u.* FROM ns_".$action." as u $condition";
    
    $tmp=getall($sql2);
    
   
    if(!empty($e_oid)){
	    foreach($tmp as $tmp2){
	    	$tmp2->attributes = array();
	    	
	    	$tmp21 = getsonall('ns_v1sampleattribute','parentId0',$tmp2->id);
	    	
	    	$tmp2->attributes = $tmp21;
	    	
	    	foreach($tmp21 as $v21){
	    	  $tmp211 = getsonall('ns_v1sampleattributeoption','parentId1',$v21->e_oid);
	    	  $v21->options =  array();
	    	  
	    	  $v21->options =$tmp211;
	    	}
	    	
	    	
	    	$result->data[] = $tmp2;
	    }
    }else{
    	    foreach($tmp as $tmp2){
	    	$result->data[] = $tmp2;
	    }   	
    }
  echo json_encode($result);   
 	
 	
 }

?>